class SettingPanel extends BasePanel {

    static COLORS_DESKTOP: string[] = ['setting_desktop_green', 'setting_desktop_blue'];
    static COLORS_PAI: string[] = ['setting_pai_blue', 'setting_pai_green', 'setting_pai_yellow'];

    private slider_music: HSliderEx;
    private btn_music: eui.CheckBox;

    private slider_sound: HSliderEx;
    private btn_sound: eui.CheckBox;

    private ddn_desktop: Dropdown;
    private ddn_pai: Dropdown;

    private lab_version: eui.Label;

    constructor() {
        super();

        this.skinName = "SettingPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.ddn_desktop.update(SettingPanel.COLORS_DESKTOP);
        this.ddn_pai.update(SettingPanel.COLORS_PAI);

        this.slider_music.addEventListener(egret.Event.CHANGE, this.changeMusicVolume, this);
        this.slider_sound.addEventListener(egret.Event.CHANGE, this.changeSoundVolume, this);

        this.slider_music.addEventListener(eui.UIEvent.CHANGE_END, this.changeMusicVolumeEnd, this);
        this.slider_sound.addEventListener(eui.UIEvent.CHANGE_END, this.changeSoundVolumeEnd, this);

        this.btn_music.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(Dropdown.CHANGED, this.dropdownHandler, this);
    }

    private dropdownHandler(sign: string) {
        if (sign == this.ddn_desktop.sign) {
            this.setDesktopStyle();
        }
        else if (sign == this.ddn_pai.sign) {
            this.setPaiColor();
        }
    }

    private changeMusicVolume(): void {
        GameStorage.setData(GameStorage.musicVolume, this.slider_music.value / 100);
        GameMusic.soundVolume = this.slider_music.value / 100;
    }

    private changeSoundVolume(): void {
        GameStorage.setData(GameStorage.soundVolume, this.slider_sound.value / 100);
    }

    private changeMusicVolumeEnd(): void {
        this.btn_music.selected = false;
        this.setMusic();
    }

    private changeSoundVolumeEnd(): void {
        this.btn_sound.selected = false;
        this.setSound();
    }

    private clickHandler(e: egret.TouchEvent) {
        GameSound.play('sound_btn_click');

        switch (e.currentTarget) {
            case this.btn_music:
                this.setMusic();
                break;
            case this.btn_sound:
                this.setSound();
                break;
        }
    }

    private setMusic(): void {
        GameStorage.setData(GameStorage.music, this.btn_music.selected ? 0 : 1);

        this.btn_music.selected ? GameMusic.stop() : GameMusic.play("music_scene");
        this.btn_music.label = this.btn_music.selected ? "点击开启" : "点击关闭";
        this.slider_music.enable = !this.btn_music.selected;
    }

    private setSound(): void {
        GameStorage.setData(GameStorage.sound, this.btn_sound.selected ? 0 : 1);

        this.btn_sound.selected && GameMusic.stop();
        this.btn_sound.label = this.btn_sound.selected ? "点击开启" : "点击关闭";
        this.slider_sound.enable = !this.btn_sound.selected;
    }

    private setDesktopStyle(): void {
        game.desktopColor = +this.ddn_desktop.selectedIndex;

        gameManager.i.dispatchEvent(GameEventType.UPDATE_DESKTOP);

        GameStorage.setData(GameStorage.desktopColor, game.desktopColor);
    }

    private setPaiColor(): void {
        game.paiColor = +this.ddn_pai.selectedIndex;

        gameManager.i.dispatchEvent(GameEventType.UPDATE_CARDCOLOR);

        GameStorage.setData(GameStorage.paiColor, game.paiColor);
    }

    show(): void {
        super.show();

        this.btn_music.selected = +GameStorage.getData(GameStorage.music) != 1;
        this.btn_sound.selected = +GameStorage.getData(GameStorage.sound) != 1;

        this.slider_music.value = +GameStorage.getData(GameStorage.musicVolume) * 100;
        this.slider_sound.value = +GameStorage.getData(GameStorage.soundVolume) * 100;

        this.btn_music.label = this.btn_music.selected ? "点击开启" : "点击关闭";
        this.btn_sound.label = this.btn_sound.selected ? "点击开启" : "点击关闭";

        this.slider_music.enable = !this.btn_music.selected;
        this.slider_sound.enable = !this.btn_sound.selected;

        this.ddn_desktop.selectedValue = SettingPanel.COLORS_DESKTOP[+GameStorage.getData(GameStorage.desktopColor)];
        this.ddn_pai.selectedValue = SettingPanel.COLORS_PAI[+GameStorage.getData(GameStorage.paiColor)];

        this.lab_version.text = "当前版本号：" + game.player.version + "    最新版本号：" + game.player.version;
    }

    static getBg(): string {
        let bg: string = '';
        switch (SettingPanel.COLORS_DESKTOP[game.desktopColor]) {
            case 'setting_desktop_green':
                bg = 'game_bg_green';
                break;
            case 'setting_desktop_blue':
                bg = 'game_bg_blue';
                break;
            default:
                break;
        }

        return bg;
    }
}